package pers.agony.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.agony.constant.MessageConstant;
import pers.agony.dao.CheckItemDao;
import pers.agony.entity.PageResult;
import pers.agony.entity.QueryPageBean;
import pers.agony.entity.Result;
import pers.agony.pojo.CheckItem;
import pers.agony.service.CheckItemService;

import java.util.List;

/**
 * @author : Agony
 * @date : Created in 8:30 下午 2021/10/31
 * @Description:
 * @Modified By:
 * @version: : 1.0
 */

@RestController
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping("/checkItem/add.do")
    public Result add(@RequestBody CheckItem checkItem){
        int rows = checkItemService.add(checkItem);
        Result result = null;
        if(rows>0){
            result = new Result(true, "添加成功！");
        }else{
            result = new Result(false,"添加失败！");
        }

        return result;
    }
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @PostMapping("/checkItem/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<CheckItem> pageResult = checkItemService.findPage(queryPageBean);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    /**
     * 删除检查项目
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @PostMapping("/checkItem/deleteById.do")
    public Result delete(int id){
        int rows = checkItemService.delete(id);
        if(rows>0){
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }else{
            return new Result(true,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @GetMapping("/checkItem/findOne.do")
    public Result findOne(@RequestParam int id){
        return checkItemService.findOne(id);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PostMapping("/checkItem/update.do")
    public Result update(@RequestBody CheckItem checkItem){
        return checkItemService.update(checkItem);
    }

}
